//
// Created by Administrator on 2021/11/2.
// May Saint Diana bless you!
//
#include <vector>
#include <iostream>
#include <algorithm>


using namespace std;

class Solution {
public:
    vector<vector<int>> groupThePeople(vector<int> &groupSizes) {
        vector<vector<int>> ans;
        unsigned int n = groupSizes.size();
        vector<bool> used(n, false);
        for (int i = 0; i < n; ++i) {
            if (used[i])
                continue;
            ans.emplace_back();
            for (int j = i; j <n && ans.back().size()<groupSizes[i] ; ++j) {
                if (groupSizes[j]==groupSizes[i] and !used[j]){
                    ans.back().push_back(j);
                    used[j] = true;
                }
            }
        }
        return ans;
        // AC
        // 也可以先按照groupSize分组，再在组内按照大小分割就行
    }
};

int main() {
    vector<int> groupSize{2,1,3,3,3,2};
    Solution solution;
    vector<vector<int>> ans = solution.groupThePeople(groupSize);
    for (auto &row: ans) {
        for (auto &x: row) {
            cout << x << " ";
        }
        cout << endl;
    }

    return 0;
}
